/**
 * Created with IntelliJ IDEA.
 * User: ajhin
 * Date: 3/19/14
 * Time: 8:12 AM
 * To change this template use File | Settings | File Templates.
 */
package com.societyGames.faceExercises
{
import flash.display.DisplayObject;
import flash.display.Sprite;

public class FaceImage extends Sprite
{
  private static const imageOrder:Vector.<String> = Vector.<String>(["Sex", "Eyes", "Glasses", "Mustache", "Hair", "Hat"]);

  [Embed(source="../../../../../FaceExercises/assets/EyesBlack.png")]
  private static const eyesBlack:Class;

  [Embed(source="../../../../../FaceExercises/assets/EyesBlue.png")]
  private static const eyesBlue:Class;

  [Embed(source="../../../../../FaceExercises/assets/SexFemale.png")]
  private static const sexFemale:Class;

  [Embed(source="../../../../../FaceExercises/assets/SexMale.png")]
  private static const sexMale:Class;

  [Embed(source="../../../../../FaceExercises/assets/MustacheBlonde.png")]
  private static const mustacheBlonde:Class;

  [Embed(source="../../../../../FaceExercises/assets/MustacheBlack.png")]
  private static const mustacheBlack:Class;

  [Embed(source="../../../../../FaceExercises/assets/Glasses.png")]
  private static const glasses:Class;

  [Embed(source="../../../../../FaceExercises/assets/HairBlackMale.png")]
  private static const hairBlackMale:Class;

  [Embed(source="../../../../../FaceExercises/assets/HairBlackFemale.png")]
  private static const hairBlackFemale:Class;

  [Embed(source="../../../../../FaceExercises/assets/HairBlondeMale.png")]
  private static const hairBlondeMale:Class;

  [Embed(source="../../../../../FaceExercises/assets/HairBlondeFemale.png")]
  private static const hairBlondeFemale:Class;

  [Embed(source="../../../../../FaceExercises/assets/HatBlue.png")]
  private static const hatBlue:Class;

  [Embed(source="../../../../../FaceExercises/assets/HatRed.png")]
  private static const hatRed:Class;

  [Embed(source="../../../../../FaceExercises/assets/Arrest.png")]
  private static const prisonHatImageClass:Class;

  [Embed(source="../../../../../FaceExercises/assets/Thief.png")]
  private static const thiefMaskImageClass:Class;

  [Embed(source="../../../../../FaceExercises/assets/Sad.png")]
  private static const sadImageClass:Class;

  private var _eyesImage:DisplayObject;

  public function FaceImage(faceData:FaceData)
  {
    for each (var key:String in FaceImage.imageOrder)
    {
      var facePartImageClass:Class;
      var trait:String = faceData.getTrait(key);
      switch (key + trait)
      {
        case "SexMale":
          facePartImageClass = sexMale;
          break;
        case "SexFemale":
          facePartImageClass = sexFemale;
          break;
        case "EyesBlack":
          facePartImageClass = null;
          _eyesImage = addFacePartImage(eyesBlack);
          break;
        case "EyesBlue":
          facePartImageClass = null;
          _eyesImage = addFacePartImage(eyesBlue);
          break;
        case "GlassesYes":
          facePartImageClass = glasses;
          break;
        case "GlassesNo":
          break;
        case "MustacheYes":
          if (faceData.getTrait("Hair") == "Black")
          {
            facePartImageClass = mustacheBlack;
          }
          else if (faceData.getTrait("Hair") == "Blonde")
          {
            facePartImageClass = mustacheBlonde;
          }
          else
          {
            throw new ArgumentError("Now hair color found.");
          }
          break;
        case "MustacheNo":
          break;
        case "HairBlack":
          if (faceData.getTrait("Sex") == "Male")
          {
            facePartImageClass = hairBlackMale;
          }
          else if (faceData.getTrait("Sex") == "Female")
          {
            facePartImageClass = hairBlackFemale;
          }
          else
          {
            throw new ArgumentError("Now sex found.");
          }
          break;
        case "HairBlonde":
          if (faceData.getTrait("Sex") == "Male")
          {
            facePartImageClass = hairBlondeMale;
          }
          else if (faceData.getTrait("Sex") == "Female")
          {
            facePartImageClass = hairBlondeFemale;
          }
          else
          {
            throw new ArgumentError("No sex found.");
          }
          break;
        case "HatBlue":
          facePartImageClass = hatBlue;
          break;
        case "HatRed":
          facePartImageClass = hatRed;
          break;
        case "HatNo":
          break;
        default:
          throw new ArgumentError("Unknown trait: " + key + " or value: " + trait);
      }
      if (facePartImageClass != null)
      {
        addFacePartImage(facePartImageClass);
      }
    }
  }

  private function addFacePartImage(imageClass:Class):DisplayObject
  {
    var newImage:DisplayObject = DisplayObject(new imageClass());
    newImage.scaleX = 2;
    newImage.scaleY = 2;
    this.addChild(newImage);
    return newImage;
  }

  public function showSad():void
  {
    if (_eyesImage)
    {
      //Find the old eyes position
      var eyeIndex:int = getChildIndex(_eyesImage);
      //Remove the old eyes
      removeChildAt(eyeIndex);
      //Put the sad image in the right place.
      var sadImage:DisplayObject = addFacePartImage(sadImageClass);
      addChildAt(sadImage, eyeIndex);
      _eyesImage = null;
    }
  }

}
}
